<?php

/**
 *
 * susan-webui - Susan Web-Frontend
 *
 * @link   https://github.com/susan/susan for the canonical source repository
 * @copyright   Copyright (c) 2013-2020 Hechzh Property (http://www.susan.org/)
 * @license   GNU Affero General Public License (http://www.gnu.org/licenses/)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

$title = 'Job details';
$this->headTitle($title);

?>

<ul class="nav nav-tabs">
   <li class="active"><a href="<?php echo $this->url('job', array('action'=>'index')); ?>"><?php echo $this->translate('Show'); ?></a></li>
   <li><a href="<?php echo $this->url('job', array('action'=>'actions')); ?>"><?php echo $this->translate('Actions'); ?></a></li>
   <li><a href="<?php echo $this->url('job', array('action'=>'run')); ?>"><?php echo $this->translate('Run'); ?></a></li>
</ul>

<br />

<?php if($this->acl_alert) : echo $this->ACLAlert($this->invalid_commands); elseif(!$this->acl_alert) : ?>

<!-- Job -->
<div class="row">

<div class="col-md-12">

<div class="panel panel-default">

<div class="panel-heading">
<h3 class="panel-title"><?php echo $this->translate("Job"); ?></h3>
</div>

<div class="panel-body">

<table class="table table-no-bordered table-hover" id="jobdetails">

<thead class="bg-primary">
   <th><?php echo $this->translate("Job ID"); ?></th>
   <th><?php echo $this->translate("Job name"); ?></th>
   <th><?php echo $this->translate("Client"); ?></th>
   <th><?php echo $this->translate("Type"); ?></th>
   <th><?php echo $this->translate("Level"); ?></th>
   <th><?php echo $this->translate("Files"); ?></th>
   <th><?php echo $this->translate("Bytes"); ?></th>
   <th><?php echo $this->translate("Errors"); ?></th>
   <th><?php echo $this->translate("Status"); ?></th>
   <th><?php echo $this->translate("Actions"); ?></th>
</thead>

</table>

</div>
</div>
</div>
</div>

<div class="row">

   <div class="col-md-8">
      <div class="panel panel-default">
         <div class="panel-heading">
            <h3 class="panel-title"><?php echo $this->translate("Messages"); ?></h3>
         </div>
         <div class="panel-body">
            <table class="table table-no-bordered table-hover" id="joblog">
               <thead class="bg-primary">
                  <th>#</th>
                  <th><?php echo $this->translate("Timestamp"); ?></th>
                  <th><?php echo $this->translate("Message"); ?></th>
               </thead>
            </table>
         </div>
      </div>
   </div>

   <div class="col-md-4">
      <div class="panel panel-default">
         <div class="panel-heading">
            <h3 class="panel-title"><?php echo $this->translate("Used Volumes"); ?></h3>
         </div>
         <div class="panel-body">
            <table class="table table-no-bordered table-hover" id="jobmedia">
               <thead class="bg-primary">
                  <th><?php echo $this->translate("Volume"); ?></th>
               </thead>
            </table>
         </div>
      </div>
   </div>

</div>


<?php
   echo $this->headScript()->prependFile($this->basePath() . '/js/custom-functions.js');
   echo $this->headLink()->prependStylesheet($this->basePath() . '/css/bootstrap-table.min.css');
   echo $this->headScript()->prependFile($this->basePath() . '/js/bootstrap-table-formatter.js');
   echo $this->headScript()->prependFile($this->basePath() . '/js/bootstrap-table-locale-all.min.js');
   echo $this->headScript()->prependFile($this->basePath() . '/js/bootstrap-table-cookie.min.js');
   echo $this->headScript()->prependFile($this->basePath() . '/js/bootstrap-table.min.js');
?>

<!-- modal-001 start -->
<div id="modal-001" class="modal fade modal-001" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
  <div class="modal-dialog modal-md">
    <div class="modal-content">
   <div class="modal-header">
   <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
   <h4 class="modal-title" id="myModalLabel"><?php echo $this->translate("Failed to retrieve data from Susan director"); ?></h4>
      </div>
      <div class="modal-body">
   <p><?php echo $this->translate("Error message received from director:"); ?></p>
   <p class="text-danger"><?php echo $this->translate("Failed to send result as json. Maybe the result message is too long?"); ?></p>
      </div>
      <div class="modal-footer">
         <button type="button" class="btn btn-default" data-dismiss="modal"><?php echo $this->translate("Close"); ?></button>
      </div>
    </div>
  </div>
</div>
<!-- modal-001 end -->

<script>

   var basePath = "<?php echo $this->basePath(); ?>";

   function detailFormatterJobDetails(index, row) {
      var html = [];
      html.push('<div class="container-fluid">');
      html.push('<table class="table table-bordered">');
      html.push('<tr>');
      html.push('<th><?php echo $this->translate("Fileset"); ?></th>');
      html.push('<td><a href="<?php echo $this->basePath() . '/fileset/details/'; ?>' + row.filesetid + '">' + row.fileset +'</a></td>');
      html.push('</tr>');
      html.push('<tr>');
      html.push('<th><?php echo $this->translate("Pool"); ?></th>');
      html.push('<td><a href="<?php echo $this->basePath() . '/pool/details/'; ?>' + row.poolname + '">' + row.poolname + '</a></td>');
      html.push('</tr>');
      html.push('<tr>');
      html.push('<th><?php echo $this->translate("Scheduled"); ?></th>');
      html.push('<td>' + row.schedtime + '</td>');
      html.push('</tr>');
      html.push('<tr>');
      html.push('<th><?php echo $this->translate("Start"); ?></th>');
      html.push('<td>' + row.starttime + '</td>');
      html.push('</tr>');
      html.push('<tr>');
      html.push('<th><?php echo $this->translate("End"); ?></th>');
      html.push('<td>' + row.endtime + '</td>');
      html.push('</tr>');
      html.push('</table>');
      html.push('</div>');
      return html.join('');
   }

   function attachJobDetailsTable() {
      $('#jobdetails').bootstrapTable({
         locale: '<?php echo str_replace('_','-', $_SESSION['susan']['locale']); ?>',
         url: '<?php echo $this->url('job', array('action' => 'getData'), null) . '?data=details&jobid='.$this->jobid; ?>',
         method: 'get',
         dataType: 'json',
         detailView: true,
         detailFormatter: 'detailFormatterJobDetails',
         columns: [
            {
               field: 'jobid',
            },
            {
               field: 'name',
               formatter: function(value) {
                  return formatJobName(value, basePath);
               }
            },
            {
               field: 'client',
               formatter: function(value) {
                  return formatClientName(value, basePath);
               }
            },
            {
               field: 'type',
               formatter: function(value) {
                  return formatJobType(value);
               }
            },
            {
               field: 'level',
               formatter: function(value) {
                  return formatJobLevel(value);
               }
            },
            {
               field: 'jobfiles',
            },
            {
               field: 'jobbytes',
               formatter: function(value) {
                  return formatBytes(value);
               }
            },
            {
               field: 'joberrors',
            },
            {
               field: 'jobstatus',
               formatter: function(value) {
                  return formatJobStatus(value);
               }
            },
            {
               field: 'action',
               formatter: function(value, row, index) {
                  return jobActionButtonsFormatter(value, row, index, basePath);
               }
            }
         ]
      });
   }

   function attachJobLogTable() {
      $('#joblog').bootstrapTable({
         locale: '<?php echo str_replace('_','-', $_SESSION['susan']['locale']); ?>',
         cookie: <?php echo $_SESSION['susan']['dt_statesave']; ?>,
         cookieIdTable: 'job_table_job_log',
         url: '<?php echo $this->url('job', array('action' => 'getData'), null) . '?data=logs&jobid='.$this->jobid; ?>',
         method: 'get',
         dataType: 'json',
         pagination : true,
         sidePagination: 'client',
         pageList: [ <?php echo $_SESSION['susan']['dt_lengthmenu']; ?> ],
         pageSize: <?php echo $_SESSION['susan']['dt_pagelength']; ?>,
         search: true,
         sortName: 'id',
         sortOrder: 'desc',
         columns: [
            { 
               field: 'id',
               sortable: true,
            },
            {
               field: 'time'
            },
            {
               field: 'logtext',
               formatter: function(value) {
                  return formatLogMessage(value);
               }
            }
         ]
      });
   }

   function attachJobMediaTable() {
      $('#jobmedia').bootstrapTable({
         locale: '<?php echo str_replace('_','-', $_SESSION['susan']['locale']); ?>',
         cookie: <?php echo $_SESSION['susan']['dt_statesave']; ?>,
         cookieIdTable: 'job_table_job_media',
         url: '<?php echo $this->url('job', array('action' => 'getData'), null) . '?data=jobmedia&jobid='.$this->jobid; ?>',
         method: 'get',
         dataType: 'json',
         pagination : true,
         sidePagination: 'client',
         pageList: [ <?php echo $_SESSION['susan']['dt_lengthmenu']; ?> ],
         pageSize: <?php echo $_SESSION['susan']['dt_pagelength']; ?>,
         search: true,
         sortName: 'volumename',
         sortOrder: 'asc',
         columns: [
            {
               field: 'volumename',
               sortable: true,
               formatter: function(value) {
                  return formatVolumeName(value, basePath);
               }
            }
         ]
      });
   }

   $(document).ready(function() {

      setDtTextDomain('<?php echo $this->basePath() . '/js/locale'; ?>');
      setDtLocale('<?php echo $_SESSION['susan']['locale']; ?>');

      attachJobDetailsTable();
      attachJobLogTable();
      attachJobMediaTable();

   });

   $('#joblog').on('load-error.bs.table', function(status, res) {
      $("#modal-001").modal();
   });

</script>

<?php endif; ?>
